% \iffalse
% Doc-Source file to use with LaTeX2e
% Copyright (C) 2004 Nicola Talbot, all rights reserved.
%<package>\NeedsTeXFormat{LaTeX2e}
%<*dtx>
\ProvidesFile{glossary.dtx}
%</dtx>
%<package>\ProvidesPackage{glossary}
% \fi
%\ProvidesFile{glossary}[2004/08/14 1.1 (NLCT)]
%\iffalse
%<*driver>
\documentclass[a4paper]{ltxdoc}
\usepackage[colorlinks,
            bookmarks,
            pdfauthor={N.L.C. Talbot},
            pdftitle={glossary.sty: A Package to Assist Generating Glossaries},
            pdfkeywords={LaTeX,package,glossary}]{hyperref}
\RecordChanges
\begin{document}
 \DocInput{glossary.dtx}
\end{document}
%</driver>
%\fi
%\CheckSum{746}
%\MakeShortVerb"
%\DeleteShortVerb{\|}
% \GetFileInfo{glossary}
% \title{glossary.sty v \fileversion: A Package to Assist Generating Glossaries}
% \author{Nicola L.C. Talbot}
% \date{14 August 2004}
% \maketitle
%\tableofcontents
%
% \section{Introduction}
% \changes{1.0}{2004 July 29}{Initial version}
% The "glossary" package is provided to assist generating a glossary.
% It has a certain amount of flexibility, allowing the user to 
% customize the format of the glossary, and define new glossary-style objects.
% 
% \section{Storing Glossary Information}
%
% The standard \LaTeX\ command "\makeglossary"\DescribeMacro{\makeglossary}{} (analogous to "\makeindex")
% should be placed in the document preamble, and glossary entries
% are generated using the command "\glossary{"\meta{key-val list}"}"\DescribeMacro{\glossary}.
% This command is a slightly modified version of the standard "\glossary"
% command, in order to separate out the information into \meta{entry-name}
% and \meta{entry-description}.  The argument to "\glossary" should be a comma-separated
% list of \meta{key}=\meta{value} pairs.  The following keys are available:
%\begin{center}
%\begin{tabular}{ll}
%name & The entry name\\
%description & A description about the entry\\
%sort & How to sort the entry. (Entry name used if sort omitted)\\
%format & How to format the page number
%\end{tabular}
%\end{center}
% For example:
%\begin{verbatim}
%\glossary{name={singular matrix},
%          description={A matrix with zero determinant}}
%\end{verbatim}
% The following example sorts on the text "U" instead of "$\mathcal{U}$":
%\begin{verbatim}
%\glossary{name={$\mathcal{U}$},
%          description={The universal set},
%          sort=U}
%\end{verbatim}
% The page format can be changed using the "format" key.  This should be
% the name of a \LaTeX\ formatting command without the preceding "\" 
% (as with the "|" operator in "\index".)  For example:
%\begin{verbatim}
%\glossary{name={$\mathbb{R}$},
%          description={The set of real numbers},
%          sort=R,
%          format=textbf}
%\end{verbatim}
%As with the "\index" command, care must be taken if you want to use the special
%characters "@", "|" and "!".  These characters should be preceded by the double quote
% character.  There is no provision for sub-entries, as these are generally only 
% applicable in an index, and not in a glossary.
%
% The glossary information is saved in the file with the extension ".glo"
%(unless the "\makeglossary" command is omitted, in which case the glossary
%information is simply ignored.)
% A "makeindex" style file (".ist") is also created, which is customized
% for the document, and can be passed to "makeindex".
%
% For example, suppose your document is called "mydoc.tex", the glossary will be saved
% in the file "mydoc.glo", and the "makeindex" style file "mydoc.ist" will be created.
% These files can then be passed to "makeindex" as follows:
%\begin{verbatim}
%makeindex -s mydoc.ist -t mydoc.glg -o mydoc.gls mydoc.glo
%\end{verbatim}
%which generates the output file "mydoc.gls", and the "makeindex" log file "mydoc.glg".
%
%The name of the ".ist" file can be changed by redefining the command
%"\istfilename"\DescribeMacro{\istfilename}.  For example:
%\begin{verbatim}
%\renewcommand{\istfilename}{foo.ist}
%\end{verbatim}
%Creation of the ".ist" file can be suppressed by issuing the command
%"\noist"\DescribeMacro{\noist}\ before "\makeglossary".  It will also be suppressed when the command "\nofiles"
% is used, or if the command "\makeglossary" is omitted.
%
%\section{Displaying the Glossary}
% Once the ".gls" file has been created by "makeindex" (as described in the previous
% section) the glossary can then be included in the document with the command "\printglossary"\DescribeMacro{\printglossary}.
% If chapters are defined, the glossary will be started with
%\begin{verbatim}
%\chapter*{\glossaryname}
%\end{verbatim}
%If not, it will be started with
%\begin{verbatim}
%\section*{\glossaryname}
%\end{verbatim}
%The format of the main body of the glossary depends on the options passed
%to the package.
%
% \section{Package Options}
% The package options must be specified as a comma-separated list of \meta{key}=\meta{value}
% pairs.  Available options are:
%\begin{description}
%\item[style] The style of "theglossary" environment. Values:
%\begin{description}
%\item[list] use description environment in the glossary
%\item[super] use supertabular environment in the glossary
%\item[long] use longtable environment in the glossary (Default)
%\end{description}
%
%\item[header] Glossary header. Values:
%\begin{description}
%\item[none] The glossary doesn't have a heading (Default)
%\item[plain] The glossary has a heading
%\end{description}
%
%\item[border] Glossary border. Values:
%\begin{description}
%\item[none] The glossary doesn't have a border (Default)
%\item[plain] Border around the main body of the glossary
%\end{description}
%
%\item[cols] Number of columns. Values:
%\begin{description}
%\item[2] The entry name and description are in two separate columns
% with the associated page numbers in the same column as the description. (Default)
%\item[3] The entry name, description and associated page numbers are
% in three separate columns.
%\end{description}
%
% \changes{1.1}{2004 August 14}{\relax added commands to omit page numbers}
% \changes{1.1}{2004 August 14}{added package option `number'}
%\item[number] Associated number corresponding to each entry.  Values:
%\begin{description}
%\item[page] Each entry has the corresponding page number(s) where the
%entry is defined. (Default)
%\item[section] Each entry has the corresponding section number(s) where
%the entry is defined.
%\item[none] The corresponding numbers are suppressed.
%\end{description}
%\end{description}
% The "border", "header" and "cols" options should not be used in conjunction
% with "style=list", as they only make sense with one of the tabular-style options.
%
%\subsection{Examples}
% Suppose the document has the following "\glossary" commands:
%\begin{center}
%\begin{tabular}{ll}
%\bfseries Page & \bfseries Command\\
%1 & "\glossary{name=diagonal matrix,"\\
%  & "          description=Matrix whose only non-zero"\\
%  & "          entries are along the leading diagonal}"\\
%2 & "\glossary{name=identity matrix,"\\
%  & "          description=Diagonal matrix with 1s along the "\\
%  & "          leading diagonal}"\\
%4 & "\glossary{name=singular matrix,"\\
%  & "          description=Matrix with zero determinant}"
%\end{tabular}
%\end{center}
%
%\begin{enumerate}
%\item If "style=list" is chosen, the glossary will look like:
%\begin{description}
%\item[diagonal matrix] Matrix whose only non-zero entries are along the leading
% diagonal, 1
%\item[identity matrix] Diagonal matrix with 1s along the leading diagonal, 2
%\item[singular matrix] Matrix with zero determinant, 4
%\end{description}
%
%\item If "style=list,number=none" is chosen, the glossary will look like:
%\begin{description}
%\item[diagonal matrix] Matrix whose only non-zero entries are along the leading
% diagonal
%\item[identity matrix] Diagonal matrix with 1s along the leading diagonal
%\item[singular matrix] Matrix with zero determinant
%\end{description}
%
%\item If "style=long,border=none,header=none,number=page" is chosen (default), the glossary will look like:\\[10pt]
%\newlength{\descriptionwidth}\setlength{\descriptionwidth}{0.6\textwidth}
%\begin{tabular}{@{\hspace{\tabcolsep}\bfseries}lp{\descriptionwidth}} &
%\\ diagonal matrix & Matrix whose only non-zero entries are along the leading
% diagonal, 1
%\\ & 
%\\identity matrix & Diagonal matrix with 1s along the leading diagonal, 2
%\\ & 
%\\ singular matrix & Matrix with zero determinant, 4
%\end{tabular}
%\\[10pt]
%
%\item If "style=long,border=plain,header=none" is chosen, the glossary will look like:
%\\[10pt]
%\begin{tabular}{|@{\hspace{\tabcolsep}\bfseries}lp{\descriptionwidth}|}\hline &
%\\ diagonal matrix & Matrix whose only non-zero entries are along the leading
% diagonal, 1
%\\ & 
%\\identity matrix & Diagonal matrix with 1s along the leading diagonal, 2
%\\ & 
%\\ singular matrix & Matrix with zero determinant, 4
%\\ & \\\hline
%\end{tabular}
%\\[10pt]
%
%\item If "style=long,border=plain,header=plain" is chosen, the glossary will look like:
%\\[10pt]
%\begin{tabular}{|@{\hspace{\tabcolsep}\bfseries}lp{\descriptionwidth}|}\hline
%\bfseries Notation & \bfseries Description\\\hline\hline &
%\\ diagonal matrix & Matrix whose only non-zero entries are along the leading
% diagonal, 1
%\\ & 
%\\identity matrix & Diagonal matrix with 1s along the leading diagonal, 2
%\\ & 
%\\ singular matrix & Matrix with zero determinant, 4
%\\ & \\\hline
%\end{tabular}
%\\[10pt]
%
%\item If "style=long,border=none,header=plain,cols=3" is chosen, the glossary will look like:
%\\[10pt]
%\begin{tabular}{@{\hspace{\tabcolsep}\bfseries}lp{\descriptionwidth}l} 
%\bfseries Notation & \bfseries Description & \\ &
%\\ diagonal matrix & Matrix whose only non-zero entries are along the leading
% diagonal & 1
%\\ & 
%\\identity matrix & Diagonal matrix with 1s along the leading diagonal & 2
%\\ & 
%\\ singular matrix & Matrix with zero determinant & 4
%\\ & \\
%\end{tabular}
%\end{enumerate}
%
%\section{Customizing the Glossary}
%The "glossary" package provides commands which can be redefined to customize the
%glossary.  The following name commands are defined by this package:
%\begin{center}\DescribeMacro{\glossaryname}\DescribeMacro{\entryname}\DescribeMacro{\descriptionname}\relax
%\begin{tabular}{ll}
%\bfseries Command & \bfseries Default Value\\
%"\glossaryname" & Glossary\\
%"\entryname" & Notation\\
%"\descriptionname" & Description
%\end{tabular}
%\end{center}
% \changes{1.1}{2004 August 14}{added \texttt{\textbackslash glossarypreamble}}%
% \changes{1.1}{2004 August 14}{added \texttt{\textbackslash glossarypostamble}}%
%Any text required before or after the glossary can be added
%by redefining the commands \DescribeMacro{\glossarypreamble}"\glossarypreamble"
%and \DescribeMacro{\glossarypostamble}"\glossarypostamble".
%For example.
%\begin{verbatim}
%\renewcommand{\glossarypreamble}{Page numbers in 
%italic indicate the main definition\par}
%\end{verbatim}
%By default, "\glossarypreamble" and "\glossarypostamble" do nothing.
%
%The start and end of the main body of the glossary is given by the commands:
%"\beforeglossary"\DescribeMacro{\beforeglossary}{} and
%"\afterglossary"\DescribeMacro{\afterglossary}.
%If the "list" style is chosen these commands simply begin and end the "description"
%environment, otherwise these commands begin and end the "longtable" or "supertabular" environment
%with argument specified by "\glossaryalignment".\DescribeMacro{\glossaryalignment}
%
%The command "\gloitem"\DescribeMacro{\gloitem}\ indicates what to do at the start of each glossary entry.
%This command takes one argument, which will be the text specified by the "name" key
%in the "\glossary" command.  In the case of the "list" style, "\gloitem{"\meta{text}"}"
%will do\\[5pt]
%"\item["\meta{text}"]"\\[5pt]
%otherwise it will do\\[5pt]
%\meta{text}~"&"\\[5pt]
%if it's the first
%item of the page or\\[5pt]
%"\\"~\meta{text}~"&"\\[5pt]
%otherwise.
%
%The command "\glodelim"\DescribeMacro{\glodelim}\ specifies what to do after the entry description and
%before the page numbers.  The default value is a comma, unless the "cols=3" option is specified,
%in which case it has the value "&".  If the package option "number=none" is
%specified, "\glodelim" will have an empty value (unless "cols=3" is specified,
%where, again, it will have the value "&".)
%
%The command "\gloskip"\DescribeMacro{\gloskip}\ specifies what to do between groups.
%If "style=list" this has the value "\indexspace", otherwise it creates a blank line
% in the "longtable" or "supertabular" environment.
%
%The command "\glossaryheader"\DescribeMacro{\glossaryheader}\ (if "style=long" or "style=super" is selected)
%indicates what to do at the start of the "longtable" or "supertabular" environment.  If "border=none", it
%is defined as:
%\begin{verbatim}
%\bfseries\entryname & \bfseries \descriptionname\\
%\end{verbatim}
%So, if you want the "\descriptionname" to be centred, you could do:
%\begin{verbatim}
%\renewcommand{\glossaryheader}{\bfseries\entryname & 
% \hfil\bfseries\descriptionname\\}
%\end{verbatim}
%The command "\glosstail"\DescribeMacro{\glosstail}\ indicates what to do at the end of
%the "longtable" or "supertabular" environment.
%
%The width of the second column for the tabular-type styles is given by the length
%"\descriptionwidth"\DescribeMacro{\descriptionwidth}.  This value can be changed using
%the "\setlength" command (the default value is "0.6\textwidth").
%
%The number associated with each entry is, by default, the page number where 
% that entry was defined.  If the package option "number=section" has been chosen, this
% will be the section number instead.  You can choose a different counter
% by changing the value of the command "\theglossarynum"\DescribeMacro{\theglossarynum}.
%For example, suppose you have the following code:
%\begin{verbatim}
%\begin{equation}
%\Gamma(z) = \int_{0}^{\infty}e^{-t}t^{z-1}\,dt
%\glossary{name=$\Gamma(z)$,description=Gamma function,sort=Gamma}
%\end{equation}
%\end{verbatim}
%and you want the glossary to refer to the equation number, instead of the
%page or section number, this can be done by redefining "\theglossarynum"
%as follows:
%\begin{verbatim}
%\renewcommand{\theglossarynum}{\theequation}
%\end{verbatim}
%If the equation number depends on another counter (such as "chapter")
%you will need to specify that the number is in the form \meta{NUM}.\meta{num}
% (e.g.\ 3.24).  This can be done by redefining the "\pagecompositor"\DescribeMacro{\pagecompositor}\ command:
%\begin{verbatim}
%\renewcommand{\pagecompositor}{.}
%\end{verbatim}
%(By default this value is "-", unless the package option "number=section" has
%been chosen, in which case this command has the value "." (full stop), as
%in the example above).  
%The "\pagecompositor" command must be
%redefined before "\makeglossary" for it to have any effect.
%
% \changes{1.1}{2004 August 14}{added \texttt{\textbackslash glsnumformat}}%
%\DescribeMacro{\glsnumformat}%
%The default format for the page number is given by "\glsnumformat" which
% takes one argument.  This format is overriden in individual glossary entries
% using the "format" key in the corresponding "\glossary" command.  You can
%redefine "\glsnumformat" before "\printglossary" to change the way the
%numbers are displayed.  For example, to make the numbers appear in a sans-serif
%font, do
%\begin{verbatim}
%\renewcommand{\glsnumformat}[1]{\textsf{#1}}
%\end{verbatim}
%
% \changes{1.1}{2004 August 14}{added \texttt{\textbackslash delimN}}
%The delimiter to be inserted between two page
%numbers for the same entry is given by "\delimN"\DescribeMacro{\delimN}.
%(This corresponds to the "delim_n" key in the "makeindex" style file.)
%By default, this has the value "{, }" (comma followed by a space).  If the
%package option "number=none" is chosen, the value is set to empty.
%
% \changes{1.1}{2004 August 14}{added \texttt{\textbackslash delimR}}
%The delimiter to be inserted between the starting and ending page
%number range for the same entry is given by "\delimR"\DescribeMacro{\delimR}.
%(This corresponds to the "delim_r" key in the "makeindex" style file.)
%By default, this has the value "--".  If the
%package option "number=none" is chosen, the value is set to empty.
%
%\section{Defining New Glossary Types}
%A new type of glossary can be defined using the command:\DescribeMacro{\newglossarytype}\\[5pt]
%"\newglossarytype{"\meta{name}"}{"\meta{out-ext}"}{"\meta{in-ext}"}"\\[5pt]
%For example, suppose you want your document to have a separate index of terms and index of notation,
%you could use "\makeglossary", "\glossary" and "\printglossary" for the first glossary, and
%define a new type of glossary called say, "notation", using
%\begin{verbatim}
%\newglossarytype{notation}{not}{ntn}
%\end{verbatim}
%which will create the analogous commands:
%"\makenotation", "\notation" and "\printnotation" which can be used for the second glossary.
%
% The command "\newglossarytype" should only occur in the preamble.  The new commands
% "\make"\meta{name}, "\"\meta{name} and "\print"\meta{name} all have the same format as their
% ``glossary'' counter-parts.  The \meta{out-ext} is the extention of the file in which the entries
% are saved (analogous to "glo").  The \meta{in-ext} is the extension of the file created by "makeindex"
% (analogous to "gls").  For the above "notation" example, if your document is called, say, "mydoc.tex",
% you will need to do the following:
%\begin{verbatim}
%latex mydoc
%makeindex -s mydoc.ist -t mydoc.glg -o mydoc.gls mydoc.glo
%makeindex -s mydoc.ist -t mydoc.nlg -o mydoc.ntn mydoc.not
%latex mydoc
%\end{verbatim}
%(You may need to do an extra "latex mydoc" to get cross-references up-to-date.)
%
%\section{Sample Documents}
%This package comes with the following sample documents:
%\begin{itemize}
%\item "sample1.tex" --- This has a simple glossary using the "list" style. You will need to do:
%\begin{verbatim}
%latex sample1
%makeindex -s sample1.ist -t sample1.glg -o sample1.gls sample1.glo
%latex sample1
%\end{verbatim}
%
%\item "sample2.tex" --- This has a glossary and defines a new glossary type called "notation".  You will
%need to do:
%\begin{verbatim}
%latex sample2
%makeindex -s sample2.ist -t sample2.glg -o sample2.gls sample2.glo
%makeindex -s sample2.ist -t sample2.nlg -o sample2.ntn sample2.not
%latex sample2
%latex sample2
%\end{verbatim}
%
%\item "sample3.tex" --- This has a glossary where the numbers in the glossary refer to the
% equation number rather than the page number.  The "\pagecompositor", "\glossaryname"
% and "\glossaryheader" are all redefined to customize the glossary. You will need to do:
%\begin{verbatim}
%latex sample3
%makeindex -s sample3.ist -t sample3.glg -o sample3.gls sample3.glo
%latex sample3
%\end{verbatim}
%
%\item "sample4.tex" --- This is a modified version of "sample2.tex".
%In this example, the glossary refers to the page number where the entry is
%defined, but the index of notation has no associated numbers.  
%You will need to do:
%\begin{verbatim}
%latex sample4
%makeindex -s sample4.ist -t sample4.glg -o sample4.gls sample4.glo
%makeindex -s sample4.ist -t sample4.nlg -o sample4.ntn sample4.not
%latex sample4
%latex sample4
%\end{verbatim}
%
%\item "sample5.tex" --- This is a modified version of "sample3.tex".
%This example has one glossary, where some of the entry numbers refer
%to the corresponding page number, and some of the entry numbers
%refer to the corresponding equation number.
%You will need to do:
%\begin{verbatim}
%latex sample5
%makeindex -s sample5.ist -t sample5.glg -o sample5.gls sample5.glo
%latex sample5
%\end{verbatim}
%\end{itemize}
%
%\section{Contact Details}
%\noindent
%Dr Nicola Talbot\\
%School of Computing Sciences\\
%University of East Anglia\\
%Norwich. Norfolk\\
%NR4 7TJ. United Kingdom.\\
%\url{http://theoval.cmp.uea.ac.uk/~nlct}
%
% \StopEventually{\PrintIndex\PrintChanges}
%
% \section{The Code}
% \subsection{Package Definition}
% First load packages needed by this package.
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%    \begin{macrocode}
\RequirePackage{ifthen}
\RequirePackage{keyval}
%    \end{macrocode}
% The package options are in the form of a comma-separated list of
% \meta{key}=\meta{value} pairs.  First need to set up the keys.
%    \begin{macrocode}
\define@key{gloss}
           {style}
           {\ifthenelse{\equal{#1}{list} \or \equal{#1}{super} \or \equal{#1}{long}}
                       {\def\gls@style{#1}}
                       {\PackageError{glossary}
                                     {Unknown glossary style '#1'}
                                     {Available styles are: list, super and long}}}

\define@key{gloss}
           {header}[plain]{\ifthenelse{\equal{#1}{none} \or \equal{#1}{plain}}
                       {\def\gls@header{#1}}
                       {\PackageError{glossary}
                                     {Unknown glossary style '#1'}
                                     {Available styles are: none and plain}}}

\define@key{gloss}
           {border}[plain]{\ifthenelse{\equal{#1}{none} \or \equal{#1}{plain}}
                       {\def\gls@border{#1}}
                       {\PackageError{glossary}
                                     {Unknown glossary border '#1'}
                                     {Available styles are: none and plain}}}
\newcount\gls@cols
\define@key{gloss}{cols}{\gls@cols=#1\relax
\ifthenelse{\gls@cols<2 \or \gls@cols>3}
           {\PackageError{glossary}
                         {invalid number of columns}
                         {The cols option can only be 2 or 3}}
           {}}

\define@key{gloss}
           {number}
           {\ifthenelse{\equal{#1}{none}\or\equal{#1}{page}\or\equal{#1}{section}}
                       {\def\gls@number{#1}}
                       {\PackageError{glossary}
                                     {Unknown glossary number style '#1'}
                                     {Available styles are: none, page and section}}}
%    \end{macrocode}
% Set up defaults
%    \begin{macrocode}
\def\gls@style{long}
\def\gls@header{none}
\def\gls@border{none}
\def\gls@number{page}
\gls@cols=2\relax
%    \end{macrocode}
% Process options
%    \begin{macrocode}

\DeclareOption*{\edef\@pkg@ptions{\noexpand\setkeys{gloss}{\CurrentOption}}
\ifthenelse{\equal{\CurrentOption}{}}{}{\@pkg@ptions}}

\ProcessOptions
%    \end{macrocode}
%Check to make sure that the options don't conflict.
%    \begin{macrocode}
\ifthenelse{\equal{\gls@style}{list} \and \(\not\equal{\gls@header}{none} \or \not\equal{\gls@border}{none} \or \gls@cols=3\)}
{\PackageError{glossary}{You can't have option 'style=list' in combination with any of the other options}
{The 'list' option doesn't have a header, border or number of columns option.}}
{}
%    \end{macrocode}
%\subsection{Redefining \texttt{\symbol{92}glossary} format}
% The glossary is going to be redefined so that it accepts \meta{key}=\meta{value}
% information, so need to define the keys (see keyval documentation for further details
% on how to do this)
%    \begin{macrocode}
\define@key{wrgloss}{name}{\def\@n@me{#1}}
\define@key{wrgloss}{description}{\def\@descr{#1}}
\define@key{wrgloss}{sort}{\def\@s@rt{#1}}
\define@key{wrgloss}{format}{\def\@f@rm@t{#1}}
%    \end{macrocode}
% Redefine "\@wrglossary" so that it separates out the entry name and 
% entry description.  Substitute "\thepage" for "\theglossarynum" which
% the user can redefine to be "\theequation" etc,
%    \begin{macrocode}
\renewcommand{\@wrglossary}[1]{\relax
\def\@n@me{}\def\@descr{}\def\@s@rt{}\def\@f@rm@t{}\relax
 \setkeys{wrgloss}{#1}\relax
 \ifthenelse{\equal{\@s@rt}{}}
    {\relax
      \ifthenelse{\equal{\@f@rm@t}{}}
      {\protected@write\@glossaryfile{}{\string\glossaryentry{\@n@me @{\@n@me}\@descr\string\relax|glsnumformat}{\theglossarynum}}}
      {\protected@write\@glossaryfile{}{\string\glossaryentry{\@n@me @{\@n@me}\@descr\string\relax|\@f@rm@t}{\theglossarynum}}}\relax
    }{\relax
      \ifthenelse{\equal{\@f@rm@t}{}}
      {\protected@write\@glossaryfile{}{\string\glossaryentry{\@s@rt @{\@n@me}\@descr\string\relax|glsnumformat}{\theglossarynum}}}
      {\protected@write\@glossaryfile{}{\string\glossaryentry{\@s@rt @{\@n@me}\@descr\string\relax|\@f@rm@t}{\theglossarynum}}}\relax
    }\relax
 \endgroup\@esphack
}
%    \end{macrocode}
%Set up default number formats, dependent on the package "number" option.
%Define default page compositor.  Any redefinition of the page compositor
%will need to come before the ".ist" file is written.  The other commands
%can be redefined at any point before "\printglossary"
%    \begin{macrocode}
\ifthenelse{\equal{\gls@number}{page}}{
   \newcommand{\theglossarynum}{\thepage}
   \newcommand{\pagecompositor}{-}
   \newcommand{\delimN}{, }
   \newcommand{\delimR}{--}
   \newcommand{\glsnumformat}[1]{#1}}
   {\ifthenelse{\equal{\gls@number}{section}}
     {\newcommand{\theglossarynum}{\thesection}
      \newcommand{\pagecompositor}{.}
      \newcommand{\delimN}{, }
      \newcommand{\delimR}{--}
      \newcommand{\glsnumformat}[1]{#1}}
     {\newcommand{\theglossarynum}{\thepage}
      \newcommand{\pagecompositor}{-}
      \newcommand{\delimN}{}
      \newcommand{\delimR}{}
      \newcommand{\glsnumformat}[1]{}}}
%    \end{macrocode}
% Now define the command "\printglossary" which will print the contents of the glossary file.
%    \begin{macrocode}
\newcommand\printglossary{\@input@{\jobname.gls}}
%    \end{macrocode}
% Define contextual names
%    \begin{macrocode}
\newcommand{\glossaryname}{Glossary}
\newcommand{\entryname}{Notation}
\newcommand{\descriptionname}{Description}
\newcommand{\istfilename}{\jobname.ist}
%    \end{macrocode}
% Now define theglossary environment
%    \begin{macrocode}
\newenvironment{theglossary}
  {\@ifundefined{chapter}
                {\section*{\glossaryname}}
                {\chapter*{\glossaryname}}
   \glossarypreamble\@bef@reglos}
  {\@ftergl@s\glossarypostamble}

\newcommand{\glossarypreamble}{}
\newcommand{\glossarypostamble}{}
%    \end{macrocode}
% Set up switch to determine whether the item is the first item in the
% glossary (in the event that a special case is needed for the first item)
%    \begin{macrocode}

\newif\ifgloitemfirst
\newcommand{\@bef@reglos}{\global\gloitemfirsttrue\beforeglossary}
\newcommand{\@ftergl@s}{\afterglossary\global\gloitemfirstfalse}
%    \end{macrocode}
% Now define what to do depending on which style has been selected.
%First deal with the "list" style, as it's the easiest.
%    \begin{macrocode}

\ifthenelse{\equal{\gls@style}{list}}
{
\newcommand{\beforeglossary}{\begin{description}}
\newcommand{\afterglossary}{\end{description}}
\newcommand{\gloskip}{\indexspace}
\newcommand{\gloitem}[1]{\item[#1]}
\newcommand{\glodelim}{, }
}{
%    \end{macrocode}
%Now deal with the other styles.  I orginally used a "tabular" environment,
%but obviously this doesn't work for a glossary longer than one page.
%Nick van Foreest recommended the "supertabular" environment.  The
% "longtable" environment also works, so have both options, and leave it to
% the user.
%    \begin{macrocode}
\ifthenelse{\equal{\gls@style}{super}}{
\IfFileExists{supertab.sty}{\RequirePackage{supertab}}
{\IfFileExists{supertabular.sty}{\RequirePackage{supertabular}}
{\PackageError{glossary}{Option "super" chosen, but can't find "supertab" package}
{If you want the "super" option, you have to have the "supertab" package installed.}}}
}
{\RequirePackage{longtable}}

\newlength{\descriptionwidth}
\setlength{\descriptionwidth}{0.6\textwidth}

\ifthenelse{\equal{\gls@header}{none}}
{
   \ifthenelse{\equal{\gls@border}{none}}
              {\newcommand{\glossaryheader}{}}
              {\newcommand{\glossaryheader}{\hline }}
}
{
\ifnum\gls@cols=2\relax
   \ifthenelse{\equal{\gls@border}{none}}
              {\newcommand{\glossaryheader}
                          {\bfseries\entryname & \bfseries \descriptionname\\}}
              {\newcommand{\glossaryheader}
                          {\hline\bfseries\entryname & \bfseries\descriptionname
                           \\\hline\hline}}
\else
   \ifthenelse{\equal{\gls@border}{none}}
              {\newcommand{\glossaryheader}
                          {\bfseries\entryname & \bfseries \descriptionname & \\}}
              {\newcommand{\glossaryheader}
                          {\hline\bfseries\entryname &\bfseries\descriptionname & 
                           \\\hline\hline}}
\fi
}

\ifthenelse{\equal{\gls@border}{none}}
{
\ifnum\gls@cols=2\relax
   \newcommand{\glossaryalignment}{@{\hspace{\tabcolsep}\bfseries}lp{\descriptionwidth}}
\else
   \newcommand{\glossaryalignment}{@{\hspace{\tabcolsep}\bfseries}lp{\descriptionwidth}l}
\fi

   \ifthenelse{\equal{\gls@style}{super}}{
      \newcommand{\afterglossary}{ \\\end{supertabular}}
   }
   {
      \newcommand{\afterglossary}{ \\\end{longtable}}
   }

   \newcommand{\glosstail}{}
}
{
\ifnum\gls@cols=2\relax
   \newcommand{\glossaryalignment}{|@{\hspace{\tabcolsep}\bfseries}lp{\descriptionwidth}|}
\else
   \newcommand{\glossaryalignment}{|@{\hspace{\tabcolsep}\bfseries}lp{\descriptionwidth}l|}
\fi

   \ifthenelse{\equal{\gls@style}{super}}{
      \newcommand{\afterglossary}{ \\\hline\end{supertabular}}
   }
   {
      \newcommand{\afterglossary}{ \\\hline\end{longtable}}
   }

   \newcommand{\glosstail}{\hline}
}

\ifthenelse{\equal{\gls@style}{super}}
{
  \newcommand{\beforeglossary}
             {\tablehead{\glossaryheader}\tabletail{\glosstail}
              \begin{supertabular}{\glossaryalignment}%
  }
}
{
  \newcommand{\beforeglossary}
             {\begin{longtable}{\glossaryalignment}
              \glossaryheader\endhead\glosstail\endfoot
  }
}

\ifnum\gls@cols=2\relax
\newcommand{\gloskip}{\ifgloitemfirst\global\gloitemfirstfalse \else\\ \fi &}
\newcommand{\glodelim}{, }
\else
\newcommand{\gloskip}{\ifgloitemfirst\global\gloitemfirstfalse \else\\ \fi & &}
\newcommand{\glodelim}{& }
\fi
\newcommand{\gloitem}[1]{\ifgloitemfirst\global\gloitemfirstfalse #1 \else \\#1 \fi &}
}

\ifthenelse{\equal{\gls@number}{none} \and \gls@cols<3}{\renewcommand{\glodelim}{}}{}
%    \end{macrocode}
% \subsection{Makeindex style file}
%This is the code to generate the ".ist" file.
%    \begin{macrocode}
\newif\ifist
\let\noist=\istfalse
\if@filesw\isttrue\else\istfalse\fi

\newwrite\istfile
\catcode`\%11\relax
\newcommand{\writeist}{
\openout\istfile=\istfilename
\write\istfile{% makeindex style file created by LaTeX for document "\jobname" on \today}
\write\istfile{keyword "\string\\glossaryentry"}
\write\istfile{preamble "\string\\begin{theglossary}"}
\write\istfile{postamble "\string\n\string\\end{theglossary}\string\n"}
\write\istfile{group_skip "\string\\gloskip "}
\write\istfile{item_0 "\string\n\string\\gloitem "}
\write\istfile{delim_0 "\string\n\string\\glodelim "}
\write\istfile{page_compositor "\pagecompositor"}
\write\istfile{delim_n "\string\\delimN"}
\write\istfile{delim_r "\string\\delimR"}
\closeout\istfile
}
\catcode`\%14\relax
%    \end{macrocode}
%Redefine "\makeglossary" so that it creates the ".ist" file.  Once it is
% created, the "\ifist" flag is set to false to prevent repeated creation of
% the file in the event that another glossary-style type is created.  If a 
% different ".ist" file is desired for each glossary type, you will need
% to precede each "\make"\meta{type} with "\isttrue" and changed the definition
% of "\istfilename".  (This is unlikely to occur unless more than one type
% of page compositor is required.)
%    \begin{macrocode}
\renewcommand{\makeglossary}{
\newwrite\@glossaryfile 
\immediate\openout\@glossaryfile=\jobname.glo 
\def\glossary{\@bsphack \begingroup \@sanitize \@wrglossary }
\typeout {Writing glossary file \jobname .glo }
\let \makeglossary \@empty
\ifist\writeist\fi
\noist}
%    \end{macrocode}
%\subsection{Defining new glossary type}
%First parameter name of new glossary type e.g.\ "notation".
%Second parameter extension of output file (equivalent to "ind" or "glo".
%Third parameter extension of input file (equivalent to "idx" or "gls").
%    \begin{macrocode}
\newcommand{\newglossarytype}[3]{
\@ifundefined{#1}{%
\def\@glstype{#1}\def\@glsout{#2}\def\@glsin{#3}%
\expandafter\edef\csname make\@glstype\endcsname{\noexpand\@m@kegl@ss{\@glstype}{\@glsout}}
\expandafter\edef\csname \@glstype\endcsname{\noexpand\@gl@ss@ary{\@glstype}}
\expandafter\edef\csname print\@glstype\endcsname{\noexpand\@prntgl@ss@ry{\@glsin}}
}{\PackageError{glossary}{Command \expandafter\string\csname #1\endcsname \space already defined}{%
You can't call your new glossary type '#1' because there already exists a command with this name}}
}
%    \end{macrocode}
% Define equivalent of "\makeglossary":
%    \begin{macrocode}
\newcommand\@m@kegl@ss[2]{
\expandafter\newwrite\csname @#1file\endcsname 
\expandafter\immediate\expandafter\openout\csname @#1file\endcsname=\jobname.#2 
\typeout {Writing #1 file \jobname .#2 }
\expandafter\let \csname make#1\endcsname \@empty
\ifist\writeist\fi
\expandafter\def\csname the#1num\endcsname{\thepage}
\noist
}
%    \end{macrocode}
% Define equivalent of "\@wrglossary":
%    \begin{macrocode}
\newcommand{\@wrgl@ss@ry}[2]{\relax
\def\@n@me{}\def\@descr{}\def\@s@rt{}\def\@f@rm@t{}\relax
 \setkeys{wrgloss}{#2}\relax
 \ifthenelse{\equal{\@s@rt}{}}
    {\relax
      \ifthenelse{\equal{\@f@rm@t}{}}
      {\expandafter\protected@write\csname @#1file\endcsname{}{\string\glossaryentry{\@n@me @{\@n@me}\@descr\string\relax|glsnumformat}{\csname the#1num\endcsname}}}
      {\expandafter\protected@write\csname @#1file\endcsname{}{\string\glossaryentry{\@n@me @{\@n@me}\@descr\string\relax|\@f@rm@t}{\csname the#1num\endcsname}}}\relax
    }{\relax
      \ifthenelse{\equal{\@f@rm@t}{}}
      {\expandafter\protected@write\csname @#1file\endcsname{}{\string\glossaryentry{\@s@rt @{\@n@me}\@descr\string\relax|glsnumformat}{\csname the#1num\endcsname}}}
      {\expandafter\protected@write\csname @#1file\endcsname{}{\string\glossaryentry{\@s@rt @{\@n@me}\@descr\string\relax|\@f@rm@t}{\csname the#1num\endcsname}}}\relax
    }\relax
 \endgroup\@esphack
}
%    \end{macrocode}
%    \begin{macrocode}
\newcommand\@gl@ss@ary[1]{\@ifundefined{@#1file}{\@bsphack\begingroup \@sanitize \@index}{\@bsphack \begingroup \@sanitize \@wrgl@ss@ry{#1}}}
%    \end{macrocode}
% Define equivalent of "\printglossary".
%    \begin{macrocode}
\newcommand\@prntgl@ss@ry[1]{\@input@{\jobname.#1}}
%    \end{macrocode}
% The command "\newglossarytype" should only be used in the preamble.
%    \begin{macrocode}

\AtBeginDocument{
\renewcommand{\newglossarytype}[3]{\@latex@error{Can only be used in preamble}\@eha}}
%    \end{macrocode}
%    \begin{macrocode}
%</package>
%    \end{macrocode}
% \Finale
